<!--
 * @Author: lwy 1245883359@qq.com
 * @Date: 2024-06-14 16:15:29
 * @LastEditors: lwy 1245883359@qq.com
 * @LastEditTime: 2024-06-15 23:24:24
 * @FilePath: \six-bird-operation-platform\src\views\financeManager\master\bail\components\bailRule.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <TheCreateForm
    ref="formRefs"
    title="保证金收取规则"
    :api="bailRuleApi"
    :options="formOption"
    :form.sync="ruleForm"
    mode="edit"
    :renderFunc="renderFunc"
    @submit="handleSubmit"
  ></TheCreateForm>
</template>

<script>
import { apiSystemConfigList, apiSystemConfig } from "@/api/bail.js";
export default {
  components: {},
  data() {
    return {
      bailRuleApi: null,
      formOption: [
        {
          type: "input-number",
          field: "orderNum",
          title: "当完成订单数超过",
          default: 10,
          placeholder: "订单数",
          min: 0,
          max: 9999999,
          precision: 0,
          controls: false,
          rules: [
            { required: true, message: "请输入订单数...", trigger: "blur" },
          ],
        },
        {
          type: "input-number",
          field: "amount",
          title: "收取保证金",
          default: 1000,
          placeholder: "保证金",
          min: 0,
          max: 9999999,
          precision: 2,
          controls: false,
          rules: [
            { required: true, message: "请输入保证金...", trigger: "blur" },
          ],
        },
      ],
      ruleForm: {},
      amount_configId: "",
      orderNum_configId: "",
    };
  },
  mounted() {},
  methods: {
    async renderFunc(data) {
      const res = await apiSystemConfigList();
      if (res.code == "SUCCESS") {
        res.data.forEach((item) => {
          if (item.configKey == "worker.security.deposit.orderNum") {
            this.ruleForm.orderNum = item.configValue;
            this.orderNum_configId = item.configId;
          } else if (item.configKey == "worker.security.deposit.amount") {
            this.ruleForm.amount =item.configValue / 100;
            this.amount_configId = item.configId;
          }
        });
        return this.ruleForm;
      }
      return data;
    },
    open() {
      this.$refs.formRefs.open();
    },
    handleSubmit(values) {
      const params = {
        configKey: "worker.security.deposit.orderNum",
        configId: this.orderNum_configId,
        configName: "保证金-收取校验-完成单数",
        configValue: values.orderNum,
      };
      const req1 = apiSystemConfig(params);
      const params2 = {
        configId: this.amount_configId,
        configKey: "worker.security.deposit.amount",
        configName: "保证金-收取校验-缴纳金额分",
        configValue: Number(values.amount * 100).toFixed(0),
      };
      const req2 = apiSystemConfig(params2);

      Promise.all([req1, req2]).then((res) => {
        console.log(res, "aa");
        this.$message({ type: "success", message: "修改成功" });
      });
    },
  },
};
</script>
